package com.serverlite;

import org.json.JSONArray;
import org.json.JSONObject;

import com.serverlite.framework.dao.SQL;
import com.serverlite.framework.dao.core.Dao;
import com.serverlite.framework.utils.PropertiesUtils;

public class Test {

    public static void main(String[] args) {
        PropertiesUtils.load("/db.properties");
        //String role_ids = "14,15,16";
        // [{"group_concat(module_ids)":"1,6,1,5,1,3"}]
//        String modules = (String) Dao
//                .queryScalar("select group_concat(module_ids) modules from system_role where id"
//                        + Dao.in(role_ids.split(",")));
//        System.out.println(modules);
        // String[] moduleIds = modules.split(",");
        // Set<String> set = new HashSet<String>();
        // for(int i = 0; i < moduleIds.length; i++){
        // set.add(moduleIds[i]);
        // }
        // JSONArray moduleArray =
        // Dao.select("select * from system_module where id " +
        // Dao.in(modules.split(",")));
        //
        // System.out.println(moduleArray);

        // JSONArray node =
        // Dao.select("select * from ("+"select * from system_module where id "
        // +
        // Dao.in(modules.split(","))+") temp where pid=? order by sort_num asc",-1);
        // System.out.println(node);

        //JSONArray ar = permissionTree(-1, role_ids);
        //JSONArray array = Dao.selectAll("system_user");
        
        //System.out.println(array);
        System.out.println(SQL.loadSql("serverlite2.sql"));
    }
    
    public static JSONArray permissionTree(int id, String roleIds){
        String modules = (String) Dao
                .queryScalar("select group_concat(module_ids) modules from system_role where id"
                        + Dao.in(roleIds.split(",")));
        return tree(id, Dao.in(modules.split(",")));
    }

    public static JSONArray tree(int id, String modules) {
        try {
            JSONArray node = Dao.select(
                "select * from (select * from system_module where id" + modules + ") temp where pid=? order by sort_num asc", id);
            for (int i = 0; i < node.length(); i++) {
                JSONObject o = node.getJSONObject(i);
                int _id = o.getInt("id");
                JSONArray children = tree(_id, modules);
                o.put("children", children);
            }
            return node;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
